//
//  ZJFCollectionWithTableViewCell.m
//  OurTourism
//
//  Created by 张金峰 on 15/11/25.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//
#define WIDTH self.contentView.frame.size.width
#import "ZJFCollectionWithTableViewCell.h"
#import "Masonry.h"
#import "ZJFWebViewController.h"
#import "ZJFQianTaoTableViewCell.h"
@interface ZJFCollectionWithTableViewCell ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@end
@implementation ZJFCollectionWithTableViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]init];
        [self.contentView addSubview:self.tableView];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(self.contentView.mas_top).offset(49);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(layoutAttributes.size.width, layoutAttributes.frame.size.height - 64 - 50 - 49));
        
    }];
    self.tableView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
  //  self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}
#pragma mark - tableView行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
    //return 10;
}
#pragma mark - tableView cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZJFQianTaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reUse"];
    if (!cell) {
        cell = [[ZJFQianTaoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reUse"];
    }
    //tableView cell接收值
    cell.guide = self.dataArr[indexPath.row];
    cell.backgroundColor = [UIColor colorWithRed:0.97 green:0.95 blue:0.91 alpha:1];
    return cell;
}

#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH / 3 ;
}

#pragma mark - cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消点击效果
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.item == 0) {
        
        //响应者链  从当前cell找到上一级tableView 上一级self.view 上一级controller
        UIResponder *tempResponder = self.nextResponder.nextResponder.nextResponder;
        ZJFWebViewController *tempViewController = [[ZJFWebViewController alloc] init];
        tempViewController.link = [self.webViewArr[indexPath.row] link];
        
        [((UIViewController *)tempResponder).navigationController pushViewController:tempViewController animated:YES];
    
    }
    else{
        self.block([self.dataArr[indexPath.row] itemid]);
    }
}
//重写赋值方法
//collectionView中嵌套了tableView  tableView每次重用数据也依然重用 需要重写赋值方法
- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (_dataArr != dataArr) {
        _dataArr = dataArr;
    }
    //重新调用创建tableView必须实现的方法
    [self.tableView reloadData];
    
}

@end
